exports.index = function (req, res) {

  //获取用户请求参数信息
  var bodyParams = req.routeInfo.bodyParams

  //当前节点ID
  var os_id = bodyParams.os_id

  //最终返回结果
  var resultData = {}

  //获取当天日期的起始
  var todayStart = new Date(new Date().toLocaleDateString()).getTime() / 1000;

  //获取当天日期的结束
  var todayEnd = (new Date(new Date().toLocaleDateString()).getTime() + 24 * 60 * 60 * 1000) / 1000;

  //查询战略合作伙伴
  function searchStrategicCooperation(cb) {

    //初始化战略存储数据
    resultData.strategicCooperationData = [];

    //如果类型为平台用户，则进行战略合作数据的查询
    if (bodyParams.u_type != '0') {
      return cb()
    }

    //查询平台下的所有战略合作伙伴
    R.SEND_HTTP(
      req,
      res,
      {
        url: CORE_APIConfig.coreAPI.organizationStructureExtensionInfoSearch.Request_Api_Url + "/os_master_type/1/os_fid/" + os_id + "/os_is_delete/0",
        method: 'POST',
        data: {
          fields: `
                  os_id,
                  os_name,
                  osei_phone
              `
        }
      },
      function (data) {

        resultData.strategicCooperationData = data.result.data;

        cb();

      }
    )
  }

  //查询战略的销售统计各项数据（新增，成交，销售额）
  function searchStrategicCooperationStatistical(cb){

    //如果类型为平台用户，则进行战略合作数据的查询
    if (bodyParams.u_type != '0') {
      return cb()
    }

    //获取要查询的年月开始与结束时间戳
    var startAndEndTime = common.getAppointBeginningEndTime(bodyParams.year,bodyParams.month);

    //循环查询战略信息
    F.ParallelEach(resultData.strategicCooperationData,function(item,index,callback){

      //查询新增客户数
      function searchAddCustomerCount(eachCB){

        R.SEND_HTTP(
          req,
          res,
          {
            url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_osid/" + item.os_id + "/c_is_delete/0/between/" + startAndEndTime.firstDate + "-c_create_time-" + startAndEndTime.endDate,
            method: 'POST',
            data: {
              fields: 'c_id'
            }
          },
          function (data) {
    
            item.customerCount = data.result.data.length;
    
            eachCB();
    
          }
        )

      }

      //查询成交与销售额
      function searchBuyCountAndBuyMoney(eachCB){

        //初始化成交总数
        var nowMonthBuyCount = 0

        //初始化成交总金额
        var nowMonthBuyMoney = 0.0

        R.SEND_HTTP(
          req,
          res,
          {
            url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_osid/" + item.os_id + "/between/" + startAndEndTime.firstDate + "-apo_create_time-" + startAndEndTime.endDate,
            method: 'POST',
            data: {
              fields: `apo_real_pay`
            }
          },
          function (data) {
    
            //计算成交总数 和 成交金额
            nowMonthBuyCount = data.result.data.length;
    
            for(var i=0;i<data.result.data.length;i++){
              nowMonthBuyMoney += parseFloat(data.result.data[i].apo_real_pay);
            }

            item.nowMonthBuyCount = nowMonthBuyCount
            item.nowMonthBuyMoney = nowMonthBuyMoney
    
            eachCB()
    
          }
        )

      }

      F.Serial([
        searchAddCustomerCount,
        searchBuyCountAndBuyMoney
      ], () => {
        
        callback()

      })

    },cb)

  }

  //查询代理商
  function searchAgent(cb) {

    //初始化代理商存储数据
    resultData.agentData = [];

    //查询平台下的所有代理商
    R.SEND_HTTP(
      req,
      res,
      {
        url: CORE_APIConfig.coreAPI.organizationStructureExtensionInfoSearch.Request_Api_Url + "/os_master_type/2/os_fid/" + os_id + "/os_is_delete/0",
        method: 'POST',
        data: {
          fields: `
                  os_id,
                  os_name,
                  osei_phone
              `
        }
      },
      function (data) {

        resultData.agentData = data.result.data;

        cb();

      }
    )
  }

  //查询代理商的销售统计各项数据（新增，成交，销售额）
  function searchAgentStatistical(cb){

    //获取要查询的年月开始与结束时间戳
    var startAndEndTime = common.getAppointBeginningEndTime(bodyParams.year,bodyParams.month);

    //循环查询战略信息
    F.ParallelEach(resultData.agentData,function(item,index,callback){

      //查询新增客户数
      function searchAddCustomerCount(eachCB){

        R.SEND_HTTP(
          req,
          res,
          {
            url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_osid/" + item.os_id + "/c_is_delete/0/between/" + startAndEndTime.firstDate + "-c_create_time-" + startAndEndTime.endDate,
            method: 'POST',
            data: {
              fields: 'c_id'
            }
          },
          function (data) {
    
            item.customerCount = data.result.data.length;
    
            eachCB();
    
          }
        )

      }

      //查询成交与销售额
      function searchBuyCountAndBuyMoney(eachCB){

        //初始化成交总数
        var nowMonthBuyCount = 0

        //初始化成交总金额
        var nowMonthBuyMoney = 0.0

        R.SEND_HTTP(
          req,
          res,
          {
            url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_osid/" + item.os_id + "/between/" + startAndEndTime.firstDate + "-apo_create_time-" + startAndEndTime.endDate,
            method: 'POST',
            data: {
              fields: `apo_real_pay`
            }
          },
          function (data) {
    
            //计算成交总数 和 成交金额
            nowMonthBuyCount = data.result.data.length;
    
            for(var i=0;i<data.result.data.length;i++){
              nowMonthBuyMoney += parseFloat(data.result.data[i].apo_real_pay);
            }

            item.nowMonthBuyCount = nowMonthBuyCount
            item.nowMonthBuyMoney = nowMonthBuyMoney
    
            eachCB()
    
          }
        )

      }

      F.Serial([
        searchAddCustomerCount,
        searchBuyCountAndBuyMoney
      ], () => {
        
        callback()

      })

    },cb)

  }

  //查询销售总数
  function searchSalesCount(cb) {

    //初始化销售数据
    resultData.salesCount = 0

    R.SEND_HTTP(
      req,
      res,
      {
        url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/no/u_is_admin-1/u_master_osid/" + os_id,
        method: 'POST',
        data: {
          fields: 'u_id'
        }
      },
      function (data) {

        resultData.salesCount = data.result.data.length;

        cb();

      }
    )
  }

  //查询总客户数
  function searchCustomerCount(cb) {

    //初始化客户总数
    resultData.customerCount = 0

    R.SEND_HTTP(
      req,
      res,
      {
        url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_osid/" + os_id + "/c_is_delete/0",
        method: 'POST',
        data: {
          fields: 'c_id'
        }
      },
      function (data) {

        resultData.customerCount = data.result.data.length;

        cb();

      }
    )

  }

  //查询总成交数和成交总金额
  function searchBuyCount(cb) {

    //初始化成交总数
    resultData.buyCount = 0

    //初始化成交总金额
    resultData.buyMoney = 0.0

    R.SEND_HTTP(
      req,
      res,
      {
        url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_osid/" + os_id,
        method: 'POST',
        data: {
          fields: `apo_real_pay`
        }
      },
      function (data) {

        //计算成交总数 和 成交金额
        resultData.buyCount = data.result.data.length;

        for(var i=0;i<data.result.data.length;i++){
          resultData.buyMoney += parseFloat(data.result.data[i].apo_real_pay);
        }

        cb()

      }
    )
  }

  //查询今日新增客户
  function nowDayAddCustomerCount(cb){

    //初始化今日新增客户数
    resultData.nowDayAddCustomerCount = 0

    R.SEND_HTTP(
      req,
      res,
      {
        url: CORE_APIConfig.coreAPI.artCustomerSearch.Request_Api_Url + "/c_osid/" + os_id + "/c_is_delete/0/between/" + todayStart + "-c_create_time-" + todayEnd,
        method: 'POST',
        data: {
          fields: 'c_id'
        }
      },
      function (data) {

        resultData.nowDayAddCustomerCount = data.result.data.length;

        cb();

      }
    )
  }

  //查询今日成交客户和成交金额
  function nowDayBuyCountAndBuyMoney(cb){

    //初始化今日成交总数
    resultData.nowDayBuyCount = 0

    //初始化今日成交总金额
    resultData.nowDayBuyMoney = 0.0

    R.SEND_HTTP(
      req,
      res,
      {
        url: config.external_server.apiAddress + "/organization/search_art_product_order/apo_osid/" + os_id + "/between/" + todayStart + "-apo_create_time-" + todayEnd,
        method: 'POST',
        data: {
          fields: `apo_real_pay`
        }
      },
      function (data) {


        //计算成交总数 和 成交金额
        resultData.nowDayBuyCount = data.result.data.length;

        for(var i=0;i<data.result.data.length;i++){
          resultData.nowDayBuyMoney += parseFloat(data.result.data[i].apo_real_pay);
        }

        cb()

      }
    )

  }


  F.Serial([
    searchStrategicCooperation,
    searchStrategicCooperationStatistical,
    searchAgent,
    searchAgentStatistical,
    searchSalesCount,
    searchCustomerCount,
    searchBuyCount,
    nowDayAddCustomerCount,
    nowDayBuyCountAndBuyMoney
  ], () => {
    RES.successResponse(res, resultData);
  })


}